﻿using System;
using System.Collections.Generic;

// 形状抽象类（父类）
abstract class Shape
{
    // 面积
    public abstract double Area();
}

// 圆形
class Circle : Shape
{
    double r;
    public Circle(double radius)
    {
        r = radius;
    }

    public override double Area()
    {
        return Math.PI * Math.Pow(r, 2);
    }
}

// 长方形
class Rectangle : Shape
{
    double width;
    double height;
    public Rectangle(double w, double h)
    {
        width = w;
        height = h;
    }

    public override double Area()
    {
        return width * height;
    }
}

// 正方形
class Square : Shape
{
    double width;
    public Square(double w)
    {
        width = w;
    }
    public override double Area()
    {
        return Math.Pow(width, 2);
    }
}

// 简单工厂
class ShapeFactory
{
    public static Shape CreateShape(string type, params double[] parameters)
    {
        switch (type.ToLower())
        {
            case "circle":
                if (parameters.Length != 1)
                    throw new ArgumentException("圆形需要提供半径参数");
                return new Circle(parameters[0]);
            case "rectangle":
                if (parameters.Length != 2)
                    throw new ArgumentException("长方形需要提供宽度和高度参数");
                return new Rectangle(parameters[0], parameters[1]);
            case "square":
                if (parameters.Length != 1)
                    throw new ArgumentException("正方形需要提供边长参数");
                return new Square(parameters[0]);
            default:
                throw new ArgumentException("未知的形状类型");
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Shape> shapes = new List<Shape>();
        Random rand = new Random();

        // 随机创建10个形状对象
        for (int i = 0; i < 10; i++)
        {
            string[] shapeTypes = { "circle", "rectangle", "square" };
            string randomType = shapeTypes[rand.Next(0, shapeTypes.Length)];
            double[] parameters;

            switch (randomType)
            {
                case "circle":
                    parameters = new double[] { rand.Next(1, 11) }; // 随机生成半径
                    break;
                case "rectangle":
                    parameters = new double[] { rand.Next(1, 11), rand.Next(1, 11) }; // 随机生成宽度和高度
                    break;
                case "square":
                    parameters = new double[] { rand.Next(1, 11) }; // 随机生成边长
                    break;
                default:
                    throw new ArgumentException("未知的形状类型");
            }

            // 使用简单工厂创建形状对象
            Shape shape = ShapeFactory.CreateShape(randomType, parameters);
            shapes.Add(shape);
        }

        // 计算面积之和
        double totalArea = 0;
        foreach (var shape in shapes)
        {
            totalArea += shape.Area();
        }

        Console.WriteLine("随机创建的10个形状对象的总面积为: " + totalArea);
    }
}
